#ifndef FORM_SK_H
#define FORM_SK_H

#include <QWidget>
#include <QSqlQuery>
#include <QPrinter>
#include <QPainter>
#include <QMap>
#include <QSqlQueryModel>
#include <QListWidgetItem>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkInterface>
#include <QNetworkRequest>
#include <QJsonObject>
#include <QJsonArray>
#include "threadyb.h"

namespace Ui {
class Form_sk;
}

class Form_sk : public QWidget
{
    Q_OBJECT

public:
    explicit Form_sk(QWidget *parent = nullptr);
    ~Form_sk();

    bool saveSI(QString invo_no); //invo_no发票号

    void uiInit();//界面初始设置
    bool ybyjs();//医保预结算
    bool putBaseIn( QJsonObject &in, QString code);

    bool scanCode();
    bool readCard();
    bool readIdCard();
    bool searchBaseData();

signals:

    void opThread(int, QString);    //用于启动医保线程

private slots:
    void closeEvent(QCloseEvent *event);
    void resizeEvent(QResizeEvent *event);
    void on_pbTc_clicked(); //退出
    void on_pbSx_clicked(); //刷新
    void on_pbSfbc_clicked();   //收费确认
    void on_pbYbjs_clicked();   //医保预结算
    bool ybjs(QString ivno);    //医保实际结算
    void on_pbPrint_clicked();   //发票打印
    void on_tvBzh_doubleClicked(const QModelIndex &index);
    void on_leBzh_returnPressed();
    void on_leSf_returnPressed();
    void onTimeOut();
    void on_rbX_clicked();
    void on_rbK_clicked();
    void on_rbM_clicked();
    void on_rbZ_clicked();

    void handleRet(const int r, const QString o1,const QString o2);
    void onTimerOut();

private:
    Ui::Form_sk *ui;
    QSqlQuery *qrm, *qro, *qro2;
    QPrinter *prt;
    QPainter *pnt;
    QTimer * tm;  //定时器
    QString tmString;   //由定时器负责显示的字串

    float ptScale;  //for printer, mm to point covnert.
    float fScale;
    QList<QMap<QString,QString>> lstFeeType;   //收费类别
    QList<QMap<QString,QString>> lstItemDetail;  //项目明细
    QList<QMap<QString, QString>> lstInvoiceInfo; //发票头信息
    QMap<QString, QString> ybRet; //医保交易返回信息
    QMap<QString,QString> mpPage;   //页面打印数据
    QMap<QString,QString> patientInfo;  //患者个人信息
    QString sq;//
    QSqlQueryModel mdBzh, mdXmmx ;  //病志号列表，项目明细列表
    //QString maxBalanceNo, remark, personType, regNo;// 从 Fin_Ipr_Siinmaininfo 读出，插入新记录时需要
    double balance;    //发票计算需要四舍五入的时候，多张发票间平衡计算余额
    bool balanceFlag;
    int balanceSeq;
    QString getInvoiceNo(QString); //检索执行科室关联的发票号
    QString getDoctCode(QString s); //检索发票关联的医生
    QStringList getFeeTypes(QString s);   //检索发票关联的费别
    QStringList getItems(QString s);   //检索发票关联的项目明细
    void printPage();



    QNetworkRequest request;
    QNetworkAccessManager manager ;
    QJsonObject ret2208setlinfo;
    QJsonObject ret2208setldetail;

    QJsonObject personBaseinfo; //1101
    QJsonObject personInsuinfo; //1101
    QJsonObject personIdetinfo; //1101
//    QJsonObject ret2201;
//    QJsonObject ret2001;
    QJsonObject ret2206setlinfo;
    QJsonObject ret2206setldetail;
    QJsonObject ret2207setlinfo;
    QJsonObject ret2207setldetail;
    QJsonArray ret2204;
    QJsonObject retScan;

    QString currentMsgid;  //最后一次msgid，用于保存医保返回结果
    QString currentInfno;  //当前的交易号
    QString msgid2207;  //结算时的msgid需要单独保存
    QString setl_id;    //结算id

    QString statusMsg;  //状态信息文本

    QThread *thread; //用于管理医保交易
    ThreadYb *ybBusiness;

    volatile bool businessFinish;    //医保交易完成标志
    QString businessRetStr1; //医保交易返回串
    QString businessRetStr2; //医保交易返回串
    int businessRetVal; //医保交易返回串
    QString cardInfo,busiCardInfo;  //卡串,业务卡串

    QList< QMap<QString,QString>> diag; //诊断
    QString chrg_bchno ;//收费批次号

    QString mdtrt_cert_type; //就诊凭证类型,交易方式，01电子凭证，02身份证，03社保卡

    bool nonInsuItem;
    bool hasInsuItem;
    double nonInsuAmount;

    QPalette palCashNormal;   //现金正常显示
    QPalette palCashUnderline;   //医保补交现金时的突出显示
};

#endif // FORM_SK_H
